aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/routes/bots/[bot_name].svelte
blob: 3532e38822c49946fe973d7db6fca93b060f95fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<script lang="ts" context="module">
  import { ApiClient } from "$lib/api_client";

  export async function load({ params, fetch }) {
    const apiClient = new ApiClient(fetch);

    try {
      const [botData, matches] = await Promise.all([
        apiClient.get(`/api/bots/${params["bot_name"]}`),
        apiClient.get("/api/matches", { bot: params["bot_name"], count: "20" }),
      ]);

      const { bot, owner, versions } = botData;
      versions.sort((a: string, b: string) =>
        dayjs(a["created_at"]).isAfter(b["created_at"]) ? -1 : 1
      );
      return {
        props: {
          bot,
          owner,
          versions,
          matches,
        },
      };
    } catch (error) {
      return {
        status: error.status,
        error: error,
      };
    }
  }
</script>

<script lang="ts">
  import dayjs from "dayjs";
  import { currentUser } from "$lib/stores/current_user";
  import MatchList from "$lib/components/matches/MatchList.svelte";

  export let bot: object;
  export let owner: object;
  export let versions: object[];
  export let matches: object[];

  // function last_updated() {
  //   versions.sort()
  // }

  // let files;

  // async function submitCode() {
  //   console.log("click");
  //   const token = get_session_token();

  //   const formData = new FormData();
  //   formData.append("File", files[0]);

  //   const res = await fetch(`/api/bots/${bot["id"]}/upload`, {
  //     method: "POST",
  //     headers: {
  //       // the content type header will be set by the browser
  //       Authorization: `Bearer ${token}`,
  //     },
  //     body: formData,
  //   });

  //   console.log(res.statusText);
  // }
</script>

<!-- 
<div>Upload code</div>
<form on:submit|preventDefault={submitCode}>
  <input type="file" bind:files />
  <button type="submit">Submit</button>
</form> -->

<div class="container">
  <div class="header">
    <h1 class="bot-name">{bot["name"]}</h1>
    {#if owner}
      <a class="owner-name" href="/users/{owner['username']}">
        {owner["username"]}
      </a>
    {/if}
  </div>

  {#if $currentUser && $currentUser["user_id"] === bot["owner_id"]}
    <div>
      <!-- TODO: can we avoid hardcoding the url? -->
      Publish a new version by pushing a docker container to
      <code>registry.planetwars.dev/{bot["name"]}:latest</code>, or using the web editor.
    </div>
  {/if}

  <div class="matches">
    <h3>Recent matches</h3>
    <MatchList {matches} />
    {#if matches.length > 0}
      <div class="btn-container">
        <a class="btn-view-more" href={`/matches?bot=${bot["name"]}`}>All matches</a>
      </div>
    {/if}
  </div>

  <!-- <div class="versions">
    <h4>Versions</h4>
    <ul class="version-list">
      {#each versions as version}
        <li>
          {dayjs(version["created_at"]).format("YYYY-MM-DD HH:mm")}
        </li>
      {/each}
    </ul>
    {#if versions.length == 0}
      This bot does not have any versions yet.
    {/if}
  </div> -->
</div>

<style lang="scss">
  .container {
    width: 800px;
    max-width: 80%;
    margin: 50px auto;
  }

  .header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    margin-bottom: 60px;
    border-bottom: 1px solid black;
  }

  $header-space-above-line: 12px;

  .bot-name {
    font-size: 24pt;
    margin-bottom: $header-space-above-line;
  }

  .owner-name {
    font-size: 14pt;
    text-decoration: none;
    color: #333;
    margin-bottom: $header-space-above-line;
  }

  $borderColor: rgba(27, 31, 36, 0.25);

  .btn-container {
    padding: 24px;
    text-align: center;
  }
  .btn-view-more {
    color: rgb(9, 105, 218);
    font-size: 14px;
    text-decoration: none;
    padding: 6px 16px;
    border: 1px solid $borderColor;
    border-radius: 5px;
  }

  .versions {
    margin: 30px 0;
  }
</style>